<?php
/*
+ --------------------------------------------------- +
|  Ineo Article Manager - Version 1.x
|  Copyright 2006-2007, Dan Cryer and William McGann
+ --------------------------------------------------- +

Ineo Article Manager is free software; you can redistribute it and/or modify
it under the terms of version 3 of the GNU General Public License as published by
the Free Software Foundation.

Ineo Article Manager is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Ineo; see the file License.txt.  If not, write to
the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.

*/

class component_public
{
	/**
	* Setup variables:
	*/
	var $ipsclass   =  null; // ip.board ipsclass      
	var $ineo_core  =  null; // ineo core class ( ip.ineo/core.php )
	var $base_url   = '';    // base url to ineo
	
	
	/**
	* Main function that's run from index.php
	*
	*/
	function run_component()
	{
		$this->base_url = $this->ipsclass->base_url . 'autocom=ineo&';
		
		require_once( ROOT_PATH . 'sources/ip.ineo/core.php' );
				
		$this->ineo_core = new ineo_core();
		$this->ineo_core->init( $this , 'ipb' , ROOT_PATH . 'sources/ip.ineo/' );
		$this->ineo_core->run();
	}
	
	/**
	* Component API function, called to add and finish output from the system:
	* @todo Fix the title to be pulled from core.
	*/
	function do_output($output)
	{
		$this->ipsclass->print->add_output( $output );
		$this->ipsclass->print->do_output( array('TITLE' => $this->ineo_core->title , 'JS' => 1, 'NAV' => $this->nav ) );
	}
    
  	/**
	* Component API function, called to load a template element
	* also doubles as a function to send ipsclass->compiled_templates
	* to the system core:
	*/
  	function load_template( $group )
  	{
      	$this->ipsclass->load_template( $group );
	  	return 'compiled_templates';
  	}
	
	/**
	* Pre Display Parse
	*
	* @since 1.0 RC1
	*/
	function display_parse( $text )
	{
		require_once( $this->ipsclass->vars['base_path'] . 'sources/handlers/han_parse_bbcode.php' ); 
	    
		$parse              = new parse_bbcode();
	    $parse->ipsclass    =& $this->ipsclass;
	    $parse->parse_html  = 0;
	    $parse->parse_nl2br = 1;
	    $parse->parse_html  = 1;
	    $text               = $parse->pre_display_parse( $text );
	
	    return $text;
	}
	
	/**
	* Pre Editor Parse
	*
	* @since 1.0 RC1
	*/
	function editor_parse( $text )
	{
		// Include parsing class
	    require_once( $this->ipsclass->vars['base_path'] . 'sources/handlers/han_parse_bbcode.php' ); 
	    $parse = new parse_bbcode();
	    $parse->ipsclass =& $this->ipsclass;
	    $parse->allow_update_caches = 0;
	    $parse->bypass_badwords = intval($this->ipsclass->member['g_bypass_badwords']);

	    // Parse the data before passing it to the editor, depends on member's editor choice
	    if ( $this->ipsclass->member['members_editor_choice'] == 'std' || $this->ipsclass->member['mgroup'] == $this->ipsclass->vars['guest_group'] )
	    {
	      $content = $parse->pre_edit_parse( $text );
	    }
	    else
	    {
	      $content = $parse->convert_ipb_html_to_html( $text );
	    }

	    return $content;
	}
	
	/**
	* Pre Database Parse
	*
	* @since 1.0 Beta 3
	*/
	function database_parse( $text )
	{
		if( empty( $this->editor ) )
		{
			// Load Editor Class:
			require_once( ROOT_PATH."sources/handlers/han_editor.php" );
			$this->editor = new han_editor();
			$this->editor->ipsclass =& $this->ipsclass;
			$this->editor->init();
		}
		
		if( empty( $this->parse ) )
		{
			// Load BBCode Parser:
			require_once( $this->ipsclass->vars['base_path'] . 'sources/handlers/han_parse_bbcode.php' ); 
		   	$this->parse = new parse_bbcode();
		   	$this->parse->ipsclass            =& $this->ipsclass;
		    $this->parse->allow_update_caches = 1;
		    $this->parse->bypass_badwords     = intval($this->ipsclass->member['g_bypass_badwords']);
		    $this->parse->parse_smilies       = 1;
		    $this->parse->parse_bbcode        = 1;
		}
		
		
		// IPB is a little silly, so let's link $_POST['text'] to $text
		$_POST['text'] =& $text;
		
		// If we're using the RTE, convert IPB HTML to Real HTML:
		if( $this->editor->method == 'rte' )
		{
			$text = $this->parse->convert_ipb_html_to_html( $text );
		}
				
    	$text = $this->parse->pre_db_parse( $this->editor->process_raw_post( 'text' ) );

		return $text;
	}
	
	function database_parse_only( $text )
	{
		if( empty( $this->parse ) )
		{
			// Load BBCode Parser:
			require_once( $this->ipsclass->vars['base_path'] . 'sources/handlers/han_parse_bbcode.php' ); 
		   	$this->parse = new parse_bbcode();
		   	$this->parse->ipsclass            =& $this->ipsclass;
		    $this->parse->allow_update_caches = 1;
		    $this->parse->bypass_badwords     = intval($this->ipsclass->member['g_bypass_badwords']);
		    $this->parse->parse_smilies       = 1;
		    $this->parse->parse_bbcode        = 1;
		}
		
		return $this->parse->pre_db_parse( $text );
	}
  
  	/**
  	* This parses the article for viewing inside of IPB. Yah mon.
  	* @todo Have it properly determine whether to parse HTML or not. Right now, it just will.
  	*/
  	function article_display_parse( $article )
  	{
		return $this->display_parse( $article );
  	}
  
  	/**
  	* Parse article's content for display in the editor
  	*/
  	function article_edit_parse( $article )
  	{
    	return $this->editor_parse( $article );
  	}
  
  	/**
  	* Load and configure the editor
  	*/
  	function edit_editor( $array )
  	{
		require_once( ROOT_PATH."sources/handlers/han_editor.php" );
		$han_editor = new han_editor();
		$han_editor->ipsclass =& $this->ipsclass;
		$han_editor->init();
		$editor_data['editor_html'] = $han_editor->show_editor( $array['content'] , 'article_content' );
		
		$editor_data['url'] = $this->ipsclass->base_url . 'autocom=ineo&mod=article&op=edit_sub&id=' . $array['id'];
		$editor_data['box_title'] = $this->ipsclass->lang['ams_editing'] . ' ' . $array['title'];
		
		return $editor_data;
  	}
}

?>